package com.ilop.sthome.mvp.present.device;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;

import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.common.command.SendDeviceCommand;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.device.AddDeviceContract;
import com.ilop.sthome.ui.activity.detail.AlarmDeviceActivity;
import com.ilop.sthome.ui.activity.detail.OutDoorDetailActivity;
import com.ilop.sthome.ui.activity.detail.THCheckDetailActivity;
import com.ilop.sthome.ui.activity.detail.lock.LockDetailActivity;
import com.ilop.sthome.ui.activity.detail.socket.SocketDetailActivity;
import com.ilop.sthome.ui.activity.detail.thermostat.TempControlDetailActivity;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.tools.ByteUtil;

/**
 * @author skygge
 * @Date on 2020-02-12.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：添加子设备业务逻辑
 */
public class AddDevicePresenter extends BasePresenterImpl<AddDeviceContract.IView> implements AddDeviceContract.IPresent {

    private Context mContext;
    private int mDeviceId;
    private String mDeviceName;
    private String mNickName;
    private String mDeviceType;
    private String mRoomId;
    private SendDeviceCommand mSendEquipment;
    private Handler mHandler;

    public AddDevicePresenter(Context mContext, String deviceName, String nickname) {
        this.mContext = mContext;
        this.mDeviceName = deviceName;
        this.mNickName = nickname;
        mHandler = new Handler();
        DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findGatewayByDeviceName(deviceName);
        mSendEquipment = new SendDeviceCommand(mContext, deviceInfoBean);
    }

    @Override
    public void onReplaceDevice(int deviceID, String deviceType) {
        mSendEquipment.replaceEquipment(deviceID, deviceType);
    }

    @Override
    public void onAddNewDevice(int deviceId, String deviceType, String bindRoom) {
        this.mDeviceId = deviceId;
        this.mDeviceType = deviceType;
        this.mRoomId = bindRoom;
        onModifyName();
    }

    @Override
    public void onModifyName() {
        if (TextUtils.isEmpty(mNickName)){
            onModifySuccess();
        }else {
            String nickName = CoderUtils.getAscii(mNickName);
            String dsCRC = ByteUtil.nameToCRC(nickName);
            mSendEquipment.modifyEquipmentName(mDeviceId, nickName + dsCRC);
        }
    }

    @Override
    public void onModifySuccess() {
        DeviceInfoBean deviceInfo = new DeviceInfoBean();
        deviceInfo.setDeviceOnly(mDeviceName + "-" + mDeviceId);
        deviceInfo.setSubdeviceName(mNickName);
        deviceInfo.setDeviceName(mDeviceName);
        deviceInfo.setDevice_type(mDeviceType);
        deviceInfo.setDevice_ID(mDeviceId);
        deviceInfo.setRoom_id(mRoomId);
        DeviceDaoUtil.getInstance().insertSubDevice(deviceInfo);
        mHandler.post(()->onSkipToDetail(mDeviceType, mDeviceId));
    }

    @Override
    public void onSkipToDetail(String deviceType, int deviceId) {
        SmartDevice mDevice = SmartDevice.getType(deviceType);
        Intent intent = new Intent();
        intent.putExtra(CommonId.KEY_DEVICE_NAME, mDeviceName);
        intent.putExtra(CommonId.KEY_DEVICE_ID, deviceId);
        switch (mDevice) {
            case EE_DEV_PIR:
            case EE_DEV_SOS:
            case EE_DEV_DOOR:
            case EE_DEV_BUTTON:
            case EE_DEV_CO_ALARM:
            case EE_DEV_SM_ALARM:
            case EE_DEV_WT_ALARM:
            case EE_DEV_GAS_ALARM:
            case EE_DEV_NEW_GAS_ALARM:
            case EE_DEV_CO_GAS_ALARM:
            case EE_DEV_MODE_BUTTON:
            case EE_DEV_SX_SM_ALARM:
            case EE_DEV_THERM_ALARM:
            case EE_DEV_FLASH_ALARM:
                intent.setClass(mContext, AlarmDeviceActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_DEV_LOCK:
                intent.setClass(mContext, LockDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_DEV_SOCKET:
            case EE_DEV_MANIPULATOR:
            case EE_DEV_SOLENOID_VALVE:
                intent.setClass(mContext, SocketDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_DEV_TH_CHECK:
                intent.setClass(mContext, THCheckDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_TEMP_CONTROL:
                intent.setClass(mContext, TempControlDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_TEMP_OUTDOOR_SIREN:
                intent.setClass(mContext, OutDoorDetailActivity.class);
                mView.skipActivity(intent);
                break;
        }
    }

    @Override
    public void onCancel() {
        mSendEquipment.cancelIncreaseEq();
    }
}
